import json
import scrapy
from testPro.items import BaiItem_Famliy,BaiQuota,BaiQuotaMap,BaiPriceMap,BaiItem_Price
import testPro.utils.RegularExpression as RE


class BaiSpider_1(scrapy.Spider):
    name = 'bai-price'
    allowed_domains = ['cloud.baidu.com']
    def __init__(self, category=None, *args, **kwargs):
        super(BaiSpider_1, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]
    # 解释器
    def parse(self, response):
        tables = response.xpath('//table')
        for y in tables:
            thead = y.xpath('thead/tr/th/text()').extract()
            for lines in y.xpath('tbody/tr'):
                line = lines.xpath('td/text()').extract()
                item = BaiItem_Price()
                # 初始化
                for value in BaiPriceMap.values():
                    item[value] = '-'

                for index, value in enumerate(line):
                    try:
                        item[BaiPriceMap[thead[index]]] = RE.find_details_translator(value)
                    except:
                        item['hour_price'] = RE.find_details_translator(value)
                yield item

class BaiSpider_2(scrapy.Spider):
    name = 'bai-family'
    allowed_domains = ['cloud.baidu.com']

    def __init__(self, category=None, *args, **kwargs):
        super(BaiSpider_2, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]
    
    def caseCPU(self, val):
        return ("处理器" in val or "CPU" in val) and "配比" not in val
    def caseProp(self, val):
        return  "配比" in val
    def caseDisk(self, val):
        return  "硬盘" in val
    def caseInsc(self, val):
        return  "指令集" in val
    def caseNet(self, val):
        return  ("网络" in val and "带宽" in val)
    # 解释器
    def parse(self, response):
        postBody = response.xpath(self.config[self.name]["XPATH-post-body"])
        postBodyUL = postBody.xpath(self.config[self.name]["XPATH-ul"])
        postBodyTitle = postBody.xpath(self.config[self.name]["XPATH-h3"])

        for i in range(len(postBodyTitle)-7):
            item = BaiItem_Famliy()
            item["Family"] = '-'
            item["CPU"] = '-'
            item["ClockSpeed"] = '-'
            item["Proportion"] = '-'
            item["Usescenes"] = '-'
            try:
                item["Family"] = RE.find_unchinese(postBodyTitle[i].xpath("string(.)").extract()[0])
                item["Family"] = RE.find_unchinesesin(item["Family"])
                item["Family"] = RE.subLowwer(item["Family"])
                data = postBodyUL[2*i].extract().split('<ul>')
                # 2.主频
                if item["Family"] in self.table[self.name].keys():
                    item["CPU"] = self.table[self.name][item["Family"]]["CPU"]
                    print(self.table[self.name][item["Family"]])
                    item["ClockSpeed"] = self.table[self.name][item["Family"]]["ClockSpeed"]
                else:
                    item["CPU"] = RE.BS_find_target(data[1],"处理器：").split('，')[0].split("：")[1]
                    item["ClockSpeed"] = RE.fing_ClockSpeed(data[1])
                item["Proportion"] = RE.find_unchinese(RE.BS_find_target(data[1],"处理器与内存配比"))
                item["Usescenes"] = postBodyUL[2*i+1].xpath("string(.)").extract()[0].replace('\n',' ')

            except:
                pass
            yield item


class BaiSpider_3(scrapy.Spider):
    name = 'bai-family-details'
    allowed_domains = ['cloud.baidu.com']

    def __init__(self, category=None, *args, **kwargs):
        super(BaiSpider_3, self).__init__(*args, **kwargs)
        with open("./testPro/conf/config.json",'r') as load_f1:
            self.config = json.load(load_f1)
        with open("./testPro/conf/data.json",'r') as load_f2:
            self.table = json.load(load_f2)
        self.start_urls = self.config[self.name]["urlList"]

    # 解释器
    def parse(self, response):
        postBody = response.xpath('//div[@class="post__body"]')
        postBodyTB = postBody.xpath('.//table')[2:]
        for i in range(len(postBodyTB)):

            thead = postBodyTB[i].xpath('./thead/tr/th/text()').extract()
            print(thead)
            for line in postBodyTB[i].xpath('./tbody/tr'):
                
                line = line.xpath('./td/text()').extract()
                item = BaiQuota()
                # 初始化
                # 初始化
                for value in BaiQuotaMap.values():
                    item[value] = '-'

                for index,value in enumerate(line):
                    temp = RE.find_unchinese(value)
                    if BaiQuotaMap[thead[index]] == "Memory_GiB":
                        temp = RE.find_num(temp)
                    
                    item[BaiQuotaMap[thead[index]]] = RE.find_details_translator(temp)
                
                yield item